// Renaming storylists
function renameList(listid) {
	var name = prompt("Rename storylist to:", null);
	if (name != null) {
		if (name == "") {
			alert("Storylist name must contain at least one character.");
		} else {
			submitRenameList(name, listid);
		}
	}
}

function submitRenameList(name, listid) {
	$.getJSON("/writeonproject/update?page=rename_list&name=" + name + "&listid=" + listid, renameListCallBack);
}

function renameListCallBack(data) {
	getMyStorylistsViewUpdates();
	if( data != null && data.error != null && data.error == "true" ) {
		alert( data.reason );
	}
}

// Delete storylist
function deleteList(listid) {
	var del = confirm("Are you sure you want to delete this storylist?");
	if (del) {
		submitDeleteList(listid);
	}
}

function submitDeleteList(listid) {
	$.getJSON("/writeonproject/update?page=delete_list&listid=" + listid, deleteListCallBack);	
}

function deleteListCallBack(data) {
	getMyStorylistsViewUpdates();
	if( data != null && data.error != null && data.error == "true" ) {
		alert( data.reason );
	}
}

// Creating storylists
function createStorylist() {
	var name = prompt("New storylist name:", null);
	if (name != null) {
		if (name == "") {
			alert("Storylist name must contain at least one character.");
		} else {
			submitCreateStorylist(name);
		}
	}
}

function submitCreateStorylist(name) {
	$.getJSON("/writeonproject/create?object=storylist&name=" + name, createStorylistCallBack);
}

function createStorylistCallBack(data) {
	getMyStorylistsViewUpdates();
	if( data != null && data.error != null && data.error == "true" ) {
		alert( data.reason );
	}
}

// Update my storylists view
function updateMyStorylistsViewInit() {
	getMyStorylistsViewUpdates();
	window.setInterval(getMyStorylistsViewUpdates, 1000);
}

function getMyStorylistsViewUpdates() {
	$.getJSON("/writeonproject/update?page=my_storylists", myStorylistsViewCallBack);
}

function myStorylistsViewCallBack(data) {
	if( data.error != null && data.error == "false") {
		var storylists = document.getElementById("mystorylists_body");
		var nostories = document.getElementById("nostories");
		if( data.storylists.length > 0 ) {
			storylists.innerHTML = "";
			for( var i = 0; i < data.storylists.length; i++ ) {
				var list = data.storylists[i];
//				var exists = document.getElementById( "list" + list.listid );
//				if( exists == null ) {
					var toprow = "<tr class=\"rowon\" id=\"list" + list.listid + "\"> <td class=\"center_col_v\">" +
							"<a href=\"/writeonproject/home?page=my_storylist&listid=" + list.listid + "\" class=\"title\">" + list.name + "</a></td>";
					toprow += "<td class=\"center_col_v center_col\">" + list.count + "</td>";
					toprow += "<td class=\"center_col\">";
					toprow += "<a href=\"" + "\" class=\"button shrink\"" +
							"onClick=\"renameList(" + list.listid + "); return false;\">" + 
							"<span><span>Rename</span></span></a> ";
					toprow += "<a href=\"" + "\" class=\"button shrink\"" +
							"onClick=\"deleteList(" + list.listid + "); return false;\">" + 
							"<span><span>Delete</span></span></a>";
					toprow += "</td></tr>";
					storylists.innerHTML += toprow;
//				}
				
				nostories.innerHTML = "";
			}
		} else {
			nostories.innerHTML = "You don't have any storylists yet! Click 'Create New Storylist' to begin using this feature.";
			storylists.innerHTML = "";
		}
	}
}

//Update storylists view
function updateStorylistsViewInit() {
	getStorylistsViewUpdates();
	window.setInterval(getStorylistsViewUpdates, 1000);
}

function getStorylistsViewUpdates() {
	var userid = document.getElementById("userid");
	$.getJSON("/writeonproject/update?page=storylists&userid=" + userid.value, storylistsViewCallBack);
}

function storylistsViewCallBack(data) {
	if( data.error != null && data.error == "false") {
		var storylists = document.getElementById("storylists_body");
		var nostories = document.getElementById("nostories");
		if( data.storylists.length > 0 ) {
			storylists.innerHTML = "";
			for( var i = 0; i < data.storylists.length; i++ ) {
				var list = data.storylists[i];
//				var exists = document.getElementById( "list" + list.listid );
//				if( exists == null ) {
					var toprow = "<tr class=\"rowon\" id=\"list" + list.listid + "\"> <td class=\"center_col_v\">" +
							"<a href=\"/writeonproject/home?page=my_storylist&listid=" + list.listid + "\" class=\"title\">" + list.name + "</a></td>";
					toprow += "<td class=\"center_col_v center_col\">" + list.count + "</td>";
					toprow += "</tr>";
					storylists.innerHTML += toprow;
//				}
				
				nostories.innerHTML = "";
			}
		} else {
			nostories.innerHTML = "This user does not have any storylists yet! Why don't you suggest that they start using this feature?";
			storylists.innerHTML = "";
		}
	}
}

function myStorylistUpdateInit() {
	getMyStorylistUpdates();
	window.setInterval(getMyStorylistUpdates, 1000 );
}

function getMyStorylistUpdates() {
	var listid = document.getElementById("listid");
	$.getJSON("/writeonproject/update?page=my_storylist&listid=" + listid.value, myStorylistsCallBack);
}

function myStorylistsCallBack(data) {
	var listid = document.getElementById("listid");
	if( data.error != null && data.error == "false" && listid != null) {
		var title = document.getElementById('mystorylist_title');
		if (title != null) {
			title.innerHTML = "My Storylist: " + data.title;
		}
		
		Cufon.replace('h2, .button, .list2 a, #sign_up a, h3', { fontFamily: 'Copse', hover:true });	
		
		var listbody = document.getElementById('storylistbody');
		var listtable = document.getElementById('storylisttable');
		var curstories = [];
		
		if( data.stories.length > 0 ) {
			for (var i = 0; i < (listtable.rows.length-1)/2; i++) {
				curstories[i] = listtable.rows[i*2 + 1].id;
			}
			
			for( var i = 0; i < data.stories.length; i++ ) {
				var story = data.stories[i];
				var storytime = document.getElementById( "time" + story.storyid );
				if( storytime == null ) {
					var bottomRow = listbody.insertRow ( 0 );
					var topRow = listbody.insertRow( 0 );
					
					topRow.className = "rowon story1";
					topRow.id = "sinfo" + story.storyid;
					var topcell1 = topRow.insertCell( 0 );
					var topcell2 = topRow.insertCell( 1 );
					var topcell3 = topRow.insertCell( 2 );
					var topcell4 = topRow.insertCell( 3 );
					var topcell5 = topRow.insertCell( 4 );
					
					topcell1.innerHTML = "<a href=\"/writeonproject/home?page=story_view&storyid=" + story.storyid +
						"\" class=\"title\">" + story.storytitle + "</a>";
					topcell2.className = "small_date";
					topcell2.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.authorid +
						"\" class=\"author\">" + story.authorid + "</a><br>" + story.createdate;
					topcell3.className = "story_post";
					topcell3.id = "pcount" + story.storyid;
					topcell3.innerHTML = story.numpost;
					topcell4.className = "small_date";
					topcell4.id = "time" + story.storyid;
					topcell4.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid +
						"\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp;
					topcell5.rowSpan = "2";
					topcell5.className = "center_col center_col_v bottom_bor";
					topcell5.innerHTML = "<a href=\"\" class=\"button shrink\" onClick=\"deleteStoryFromList(" + 
						story.storyid + "," + listid.value + ");return false;\"><span><span>Delete</span></span></a>";
					
					bottomRow.className = "rowon";
					var bottom1 = bottomRow.insertCell( 0 );
					bottom1.className = "story_preview";
					bottom1.id = "sp" + story.storyid;
					bottom1.colSpan = "4";
					bottom1.innerHTML = story.preview;
				} else {
					var storyts = document.getElementById( "time" + story.storyid );
					storyts.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp; 
					
					var preview = document.getElementById( "sp" + story.storyid );
					preview.innerHTML = story.preview;
					
					var storyp = document.getElementById( "pcount" + story.storyid );
					storyp.innerHTML = story.numpost;
				}
				
				var index = curstories.indexOf("sinfo" + story.storyid);
				if (index != -1) {
					curstories.splice(index, 1);
				}
			}
		}
		
		for (var i = 0; i < (listtable.rows.length-1)/2; i++) {
			var id = listtable.rows[i*2 + 1].id;
			
			if (curstories.indexOf(id) != -1) {
				listtable.deleteRow(i*2 + 2);
				listtable.deleteRow(i*2 + 1);
			}
		}
		
		if (listtable.rows.length == 1) {
			document.getElementById("nostories").innerHTML = "<br>Oh no! You don't have any stories in this storylist!";
		} else {
			document.getElementById("nostories").innerHTML = "";
		}
	}
}

function deleteStoryFromList(storyid, listid) {
	if (confirm("Are you sure you want to delete this story from your storylist?")) {
	
		$.getJSON("/writeonproject/update?page=my_storylist_delete&listid=" + listid + 
				"&storyid=" + storyid, deleteStoryFromListCallBack);
	}
}

function deleteStoryFromListCallBack(data) {
	if (data != null && data.error != null && data.error == "true") {
		alert( data.reason );
	} else {
		getMyStorylistUpdates();
	}
}

function shareStorylist(listid) {
	var hasEmail = 0;
	while (hasEmail == 0) {
		var email = prompt("Enter the email of who you want to share it with:", null);
		var atpos = email.indexOf("@");
		var dotpos = email.lastIndexOf(".");
		if (atpos < 1 || dotpos < atpos+2 || dotpos+2 >= email.length || email == null || email == "") {
			alert("Please enter a valid e-mail address.");
		} else {
			hasEmail = 1;
			$.getJSON("/writeonproject/create?object=sharestorylist&listid=" + listid + "&email=" + email, shareCallBack);
		}
	}

//		submitCreateStorylist(name);

}

function shareCallBack(data) {
	if( data.error != null ) {
		alert(data.reason);
	}
}

function storylistUpdateInit() {
	getStorylistUpdates();
	window.setInterval(getStorylistUpdates, 1000 );
}

function getStorylistUpdates() {
	var listid = document.getElementById("listid");
	$.getJSON("/writeonproject/update?page=storylist&listid=" + listid.value, storylistsCallBack);
}

function storylistsCallBack(data) {
	if( data.error != null && data.error == "false" ) {
		var listbody = document.getElementById('storylistbody');
		var listtable = document.getElementById('storylisttable');
		var curstories = [];
		
		var title = document.getElementById('storylist_title');
		if (title != null) {
			title.innerHTML = data.userid + "'s Storylist: " + data.title;		
		}
		
		Cufon.replace('h2, .button, .list2 a, #sign_up a, h3', { fontFamily: 'Copse', hover:true });	
		
		if( data.stories.length > 0 ) {
			for (var i = 0; i < (listtable.rows.length-1)/2; i++) {
				curstories[i] = listtable.rows[i*2 + 1].id;
			}
			
			for( var i = 0; i < data.stories.length; i++ ) {
				var story = data.stories[i];
				var storytime = document.getElementById( "time" + story.storyid );
				if( storytime == null ) {
					var bottomRow = listbody.insertRow ( 0 );
					var topRow = listbody.insertRow( 0 );
					
					topRow.className = "rowon story1";
					topRow.id = "sinfo" + story.storyid;
					var topcell1 = topRow.insertCell( 0 );
					var topcell2 = topRow.insertCell( 1 );
					var topcell3 = topRow.insertCell( 2 );
					var topcell4 = topRow.insertCell( 3 );

					topcell1.innerHTML = "<a href=\"/writeonproject/home?page=story_view&storyid=" + story.storyid + "\" class=\"title\">" + story.storytitle + "</a>";
					topcell2.className = "small_date";
					topcell2.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.authorid + "\" class=\"author\">" + story.authorid + "</a><br>" + story.createdate;
					topcell3.className = "story_post";
					topcell3.id = "pcount" + story.storyid;
					topcell3.innerHTML = story.numpost;
					topcell4.className = "small_date";
					topcell4.id = "time" + story.storyid;
					topcell4.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp;

					bottomRow.className = "rowon";
					var bottom1 = bottomRow.insertCell( 0 );
					bottom1.className = "story_preview";
					bottom1.id = "sp" + story.storyid;
					bottom1.colSpan = "4";
					bottom1.innerHTML = story.preview;
				} else {
					var storyts = document.getElementById( "time" + story.storyid );
					storyts.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp; 
					
					var preview = document.getElementById( "sp" + story.storyid );
					preview.innerHTML = story.preview;
					
					var storyp = document.getElementById( "pcount" + story.storyid );
					storyp.innerHTML = story.numpost + 1;
				}
				
				var index = curstories.indexOf("sinfo" + story.storyid);
				if (index != -1) {
					curstories.splice(index, 1);
				}
			}
		}
		
		for (var i = 0; i < (listtable.rows.length-1)/2; i++) {
			var id = listtable.rows[i*2 + 1].id;
			
			if (curstories.indexOf(id) != -1) {
				listtable.deleteRow(i*2 + 2);
				listtable.deleteRow(i*2 + 1);
			}
		}
		
		if (listtable.rows.length <= 1) {
			document.getElementById("nostories").innerHTML = "<br>Oh no! This user doesn't have any stories in this storylist!";
		} else {
			document.getElementById("nostories").innerHTML = "";
		}
	}
}

function checkUserIsOwner(user, listid) {
	$.getJSON("/writeonproject/update?page=check_list_owner&listid=" + listid + "&userid=" + userid, checkUserIsOwnerCallBack);	
}

function checkUserIsOwnerCallBack(data) {
	if (data.error != null && data.error == "false") {
		if (window.location.href.indexOf("my_storylist") != -1 && data.owner == "false") {
			window.location("/writeonproject/home?page=storylist&listid=" + data.listid);
		} else if (window.location.href.indexOf("my_storylist") == -1 && data.owner == "true") {
			window.location("/writeonproject/home?page=my_storylist&listid=" + data.listid);			
		}
	}
}

function checkIfUserExists(userid) {
	$.getJSON("/writeonproject/update?page=user_check&userid=" + userid, checkIfUserExistsCallBack);	
}

function checkIfUserExistsCallBack(data) {
	if (data.error != null && data.error == false) {
		if (data.exists == "false") {
			window.location("/writeonproject/home");
		}
	}
}